package rank

import (
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"server/db"
	"server/game/player"
	playerdb "server/game/player/db"
	"server/util"
)

const (
	CharmRankCollection   = "t_charm_rank"
	IncomeRankCollection  = "t_income_rank"
	GuildRankCollection   = "t_guild_rank"
	AbilityRankCollection = "t_ability_rank"
)

type RankDao struct {
	db *db.MongoMiddleware
}

func NewRankDao() *RankDao {
	return &RankDao{
		db: db.Db,
	}
}

func (r *RankDao) Inster() {
	var playerList []player.Player
	r.db.Find(playerdb.USERTABLENAME, bson.M{}, &playerList)
	for _, playerInfo := range playerList {
		abilityRank := AbilityRank{
			Ability:    int64(util.GetRandomInt(10, 90)),
			PlayerID:   playerInfo.UID,
			Level:      playerInfo.Level,
			PlayerName: playerInfo.PlayerName,
			Avatar:     playerInfo.Avatar,
			Title:      playerInfo.Title,
		}
		r.db.Client.Collection(AbilityRankCollection).DropCollection(context.Background())
		r.db.Insert(AbilityRankCollection, abilityRank)
		incomeRank := IncomeRank{
			Income:     int64(util.GetRandomInt(10, 90)),
			PlayerID:   playerInfo.UID,
			Level:      playerInfo.Level,
			PlayerName: playerInfo.PlayerName,
			Avatar:     playerInfo.Avatar,
			Title:      playerInfo.Title,
		}
		r.db.Client.Collection(IncomeRankCollection).DropCollection(context.Background())
		r.db.Insert(IncomeRankCollection, incomeRank)
		charmRank := CharmRank{
			Charm:      int64(util.GetRandomInt(10, 90)),
			PlayerID:   playerInfo.UID,
			Level:      playerInfo.Level,
			PlayerName: playerInfo.PlayerName,
			Avatar:     playerInfo.Avatar,
			Title:      playerInfo.Title,
		}
		r.db.Client.Collection(CharmRankCollection).DropCollection(context.Background())
		r.db.Insert(CharmRankCollection, charmRank)
	}
}
func (r *RankDao) getAbilityRank() []AbilityRank {
	var ranks []AbilityRank
	ctx := context.Background()
	r.db.Client.Collection(AbilityRankCollection).Find(ctx, bson.D{}).Sort("ability").Limit(100).All(&ranks)
	return ranks
}
func (r *RankDao) getIncomeRank() []IncomeRank {
	var ranks []IncomeRank
	ctx := context.Background()
	r.db.Client.Collection(IncomeRankCollection).Find(ctx, bson.D{}).Sort("income").Limit(100).All(&ranks)
	return ranks
}
func (r *RankDao) getCharmRank() []CharmRank {
	var ranks []CharmRank
	ctx := context.Background()
	r.db.Client.Collection(CharmRankCollection).Find(ctx, bson.D{}).Sort("charm").Limit(100).All(&ranks)
	return ranks
}
func (r *RankDao) getGuildRank() []GuildRank {
	var ranks []GuildRank
	ctx := context.Background()
	r.db.Client.Collection(GuildRankCollection).Find(ctx, bson.D{}).Sort("income").Limit(100).All(&ranks)
	return ranks
}
